package com.lovo.order.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.management.Query;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class MQConfig {
    //注入工厂（已经预加载）
    @Autowired
    CachingConnectionFactory connectionFactory;
    //rabbitmq模板
    @Bean(value = "rabbitTemplate")
    public RabbitTemplate rabbitTemplate(){

        return new RabbitTemplate(connectionFactory);
    }
    //创建普通队列
// @Bean
// public Queue createPointQueue(){
//        return new Queue("lcq_point");
// }


//
//    @Bean
//     public  Queue createTopicQueue(){
//        return new Queue("j177_topic");
//    }

    //死信队列
    @Bean
    public  Queue deadQueue(){
        return new Queue("deadQueue");
    }
    //死信交换机
    @Bean
    public DirectExchange deadExchange(){
        return  new DirectExchange("deadExchange");
    }
//
    //死信交换机队列绑定
    @Bean
    public Binding deadQueueToDeadExchange(Queue deadQueue,DirectExchange deadExchange){
        return  BindingBuilder.bind(deadQueue).to(deadExchange).with("dead_routing_key");
    }

    @Bean
    public Queue createPointQueue(){
        //关联死信交换机
        Map<String, Object> args = new HashMap<>(3);
        // x-dead-letter-exchange    这里声明当前队列绑定的死信交换机
        args.put("x-dead-letter-exchange", "deadExchange");
        // x-dead-letter-routing-key  这里声明当前队列的死信路由key
        args.put("x-dead-letter-routing-key","dead_routing_key");
        // x-message-ttl  声明队列的TTL
        args.put("x-message-ttl",30000);
        return QueueBuilder.durable("lcq_point").withArguments(args).build();
    }

    //创建交换机
    @Bean
    public DirectExchange directExchange(){
        return new DirectExchange("directExchange");
    }
    // 主题交换机
//    @Bean
//    public TopicExchange topicExchange(){
//        return new TopicExchange("topicExchange");
//    }

    //扇型交换机
//    @Bean
//     public  FanoutExchange fanoutExchange(){
//        return new FanoutExchange("fanoutExchange");
//     }

    //把队列绑定到交换机
    @Bean
    public Binding createPointQueueToDirectExchange(
            Queue createPointQueue,
            DirectExchange directExchange){
      return BindingBuilder.bind(createPointQueue).to(directExchange)
              .with("lcq.point");
    }
//    @Bean
//     public Binding createTopicQueueToTopicExchange(
//             Queue createTopicQueue,
//             TopicExchange topicExchange){
//          return BindingBuilder.bind(createTopicQueue).to(topicExchange).with("topic.#");
//     }

     //绑定扇型交换机
//    @Bean
//    public Binding createPointQueueToFanoutExchange(
//            Queue createPointQueue,
//            FanoutExchange fanoutExchange){
//        return BindingBuilder.bind(createPointQueue).to(fanoutExchange);
//    }
//    @Bean
//    public Binding createTopicQueueToFanoutExchange(
//            Queue createTopicQueue,
//            FanoutExchange fanoutExchange){
//        return BindingBuilder.bind(createTopicQueue).to(fanoutExchange);
//    }

}
